#!/bin/bash

set -e

export ANDROID_HOME=~/Android/Latest/Sdk
export ANDROID_NDK_HOST=linux-x86_64
export ANDROID_NDK_PLATFORM=android-23
export ANDROID_NDK_TOOLCHAIN_VERSION=4.9
export ANDROID_NDK_ROOT=$ANDROID_HOME/ndk/23.1.7779620
export ANDROID_SDK_ROOT=$ANDROID_HOME

export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-amd64
export PATH=$PATH:$ANDROID_HOME/Sdk/tools:/usr/bin:$JAVA_HOME/bin

rm -rf build/android/*

export PATH=/opt/Qt5/5.15.2/android/bin/:$PATH

# Mobile
qmake -config release "CONFIG += release_android build_mobile" ANDROID_ABIS="arm64-v8a" -spec android-clang
make clean
make -j8
make install INSTALL_ROOT=build/android/build
androiddeployqt --gradle --no-gdbserver --output build/android/build --input android-vesc_tool-deployment-settings.json --android-platform android-33
mv build/android/build/build/outputs/apk/debug/build-debug.apk build/android/vesc_tool_mobile.apk
rm -rf build/android/build
rm -rf build/android/obj
rm -f build/android/libvesc_tool*

# Full
qmake -config release "CONFIG += release_android" ANDROID_ABIS="arm64-v8a" -spec android-clang
make clean
make -j8
make install INSTALL_ROOT=build/android/build
androiddeployqt --gradle --no-gdbserver --output build/android/build --input android-vesc_tool-deployment-settings.json --android-platform android-33
mv build/android/build/build/outputs/apk/debug/build-debug.apk build/android/vesc_tool_full.apk
rm -rf build/android/build
rm -rf build/android/obj
rm -f build/android/libvesc_tool*

# Zip APKs
cd build/android
zip vesc_tool_android.zip vesc_tool_mobile.apk vesc_tool_full.apk
rm -f vesc_tool_mobile.apk
rm -f vesc_tool_full.apk
cd ../..

